package com.wxdc.nmgyt.nmgytwydc.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wxdc.nmgyt.nmgytwydc.R;
import com.wxdc.nmgyt.nmgytwydc.app.MyApp;
import com.wxdc.nmgyt.nmgytwydc.common.AddWzAndroidInterface;
import com.wxdc.nmgyt.nmgytwydc.model.CbYfInfoBean;
import com.wxdc.nmgyt.nmgytwydc.model.GmYfInfoBean;
import com.wxdc.nmgyt.nmgytwydc.model.PicUploadStateBean;
import com.wxdc.nmgyt.nmgytwydc.model.YdInfoBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.YdAddresultBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.YdEditResultBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.YfEditResultBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.YfRemoveReusltBean;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * Created by Administrator on 2018/8/31.
 */

public class AddWzActivity extends BaseWebX5Activity {
    private final int REQUEST_CODE_CHOOSE = 0x110201;//定义请求码常量
    private final String TAG = this.getClass().getSimpleName();
    private Context mContext = this;
    @BindView(R.id.addwz_toolbar_title)
    TextView mAddwzToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.addwz_webcontainer)
    LinearLayout mAddwzWebcontainer;
    private Unbinder mUnbinder;
    private String htmlUrl = "";
    private List<String> selectPicUrls;//保存相册选中的图片地址

    @Override
    protected int getLayout() {
        return R.layout.activity_addwz;
    }

    @Override
    protected void initInject() {
        getActivityComponent().Inject(this);
    }

    @Override
    public String getUrl() {
        htmlUrl = MyApp.getInstance().getHTTPWz();
        return super.getUrl();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUnbinder = ButterKnife.bind(this);
        mToolbar.setTitleTextColor(Color.WHITE);
        mToolbar.setTitle("");
        this.setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
        super.initAgentWeb(mAddwzWebcontainer, mAddwzToolbarTitle, getUrl());
        if (mAgentWebX5 != null) {
            //注入对象
            mAgentWebX5.getJsInterfaceHolder().addJavaObject("android", new AddWzAndroidInterface(mAgentWebX5, this, this));
        }
    }

    /**
     * 打开相机或相册
     *
     * @param selectPicNum
     */
    public void openAlbumAndCamera(int selectPicNum) {
        Matisse.from(AddWzActivity.this)
                .choose(MimeType.ofAll(), false)//图片类型
                .countable(true)//true:选中后显示数字;false:选中后显示对号
                .capture(true)//选择照片时，是否显示拍照
                .captureStrategy(new CaptureStrategy(true, "com.wxdc.nmgyt.nmgytwydc.fileprovider"))//参数1 true表示拍照存储在共有目录，false表示存储在私有目录；参数2与 AndroidManifest中authorities值相同，用于适配7.0系统 必须设置
                .maxSelectable(selectPicNum)//可选的最大数
                .theme(R.style.Matisse_Zhihu)//主题  暗色主题 R.style.Matisse_Dracula
                .originalEnable(true)
                .maxOriginalSize(10)
                .imageEngine(new GlideEngine())//图片加载引擎
                .forResult(REQUEST_CODE_CHOOSE);//请求码
    }

    /**
     * 重写onActivityResult方法
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) { //相机或相册返回结果
            selectPicUrls = Matisse.obtainPathResult(data);
            //如果拍照更新到图库
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.parse("file://" + Matisse.obtainPathResult(data).get(0));
            intent.setData(uri);
            Log.i(TAG, "onActivityResult: -------uri---" + uri);
            mContext.sendBroadcast(intent);
        }
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AddWzActivity.class);
        context.startActivity(intent);
    }
    //==============================生命周期========================================================

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
    //--------------------------------------------------
    @Override
    public void showError(String msg) {

    }

    @Override
    public void singlePicUploadResult(PicUploadStateBean results) {

    }

    @Override
    public void multiPicUploadResult(List<PicUploadStateBean> result) {
        
    }

    @Override
    public void isYdInfoExist(ResponseBody responseBody) {

    }

    @Override
    public void addYdInfoResult(YdAddresultBean addresultBean) {

    }

    @Override
    public void editYdInfoResult(YdAddresultBean resultBean) {

    }

    @Override
    public void findCbyfInfoResults(List<CbYfInfoBean> cbYfInfoBeanList) {

    }

    @Override
    public void addCbyfInfoResult(YfEditResultBean result) {

    }

    @Override
    public void editCbyfInfoResult(YfEditResultBean result) {

    }

    @Override
    public void removeCbyfInfoResult(YfRemoveReusltBean result) {

    }

    @Override
    public void findGmyfInfoResults(List<GmYfInfoBean> gmYfInfoBeanList) {

    }

    @Override
    public void addGmyfInfoResult(YfEditResultBean result) {

    }

    @Override
    public void editGmyfInfoResult(YfEditResultBean result) {

    }

    @Override
    public void removeGmyfInfoResult(YfRemoveReusltBean result) {

    }
    //==============================================================================================
}
